package org.karelun.gef.trash;

import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.draw2d.Viewport;
import org.eclipse.draw2d.parts.ScrollableThumbnail;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.LayerConstants;
import org.eclipse.gef.editparts.ScalableRootEditPart;
import org.eclipse.gef.ui.actions.ActionRegistry;
import org.eclipse.gef.ui.parts.ContentOutlinePage;
import org.eclipse.gef.ui.parts.SelectionSynchronizer;
import org.eclipse.gef.ui.parts.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.part.IPageSite;
import org.karelun.gef.outline.TreePartFactory;

public class OutlinePage2 extends ContentOutlinePage{

	private SashForm sash;

	private ScrollableThumbnail thumbnail;

	private DisposeListener disposeListener;
	
	private UMLEditor editor;
	
	private SelectionSynchronizer selectionSyn;

	private ActionRegistry actionReg;

	private GraphicalViewer viewer;
	
	public OutlinePage2(UMLEditor editor) {
		super(new TreeViewer());
		this.editor = editor;
		selectionSyn = editor.getSelectionSynchronizer();
		actionReg = editor.getActionRegistry();
		viewer = editor.getGraphicalViewer();
	}
	@SuppressWarnings("deprecation")
	public void init(IPageSite pageSite) {
		super.init(pageSite);

		IActionBars bars = pageSite.getActionBars();
		String id = IWorkbenchActionConstants.UNDO;
		bars.setGlobalActionHandler(id, actionReg.getAction(id));

		id = IWorkbenchActionConstants.REDO;
		bars.setGlobalActionHandler(id, actionReg.getAction(id));

		id = IWorkbenchActionConstants.DELETE;
		bars.setGlobalActionHandler(id, actionReg.getAction(id));

		bars.updateActionBars();
	}

	public void createControl(Composite parent) {
		sash = new SashForm(parent, SWT.VERTICAL);
		getViewer().createControl(sash);
		getViewer().setEditDomain(viewer.getEditDomain());
		getViewer().setEditPartFactory(new TreePartFactory());
		getViewer().setContents(editor.getDiagram());

		selectionSyn.addViewer(getViewer());

		Canvas canvas = new Canvas(sash, SWT.BORDER);
		LightweightSystem lws = new LightweightSystem(canvas);

		thumbnail = new ScrollableThumbnail(
				(Viewport) ((ScalableRootEditPart) viewer.getRootEditPart())
						.getFigure());
		thumbnail.setSource(((ScalableRootEditPart) viewer.getRootEditPart())
				.getLayer(LayerConstants.PRINTABLE_LAYERS));

		lws.setContents(thumbnail);

		disposeListener = new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				if (thumbnail != null) {
					thumbnail.deactivate();
					thumbnail = null;
				}
			}
		};
		viewer.getControl().addDisposeListener(disposeListener);
	}

	public Control getControl() {
		return sash;
	}

	public void dispose() {
		selectionSyn.removeViewer(getViewer());
		Control control = viewer.getControl();
		if (control != null && !control.isDisposed()) {
			control.removeDisposeListener(disposeListener);
		}
		super.dispose();
	}
}
